//
//  GFOtherFoodViewController.m
//  GoodFood
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFOtherFoodViewController.h"

@interface GFOtherFoodViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, copy) NSString *myId;//id
@property (nonatomic, assign) NSInteger page;//页数
@property (nonatomic, strong) NSMutableArray *arrayForFood;//推荐食物列表数组
@property (nonatomic, strong) UICollectionView *foodListCollectionView;//食物列表的collectionView
@property (nonatomic, strong) NSArray *arrayForSort;//食物列表前3名
@end

@implementation GFOtherFoodViewController
#pragma mark - 初始化
- (instancetype)initWithId:(NSString *)myId withSort:(NSArray *)array {
    self = [super init];
    if (self) {
        self.myId = myId;
        self.page = 1;
        self.arrayForSort = array;
    }
    return self;
}

#pragma mark - 导航
- (void)viewForNavi {
    UIView *view = (UIView *)[self.navigationController.view viewWithTag:10001];
    view.backgroundColor = [UIColor redColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth / 2, ScreenHeight / 3, ScreenWidth / 3, ScreenHeight / 4)];
    label.text = @"其他推荐";
    label.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.7];
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = label;
    self.view.backgroundColor = [UIColor lightGrayColor];
}
#pragma mark - 数据解析
- (void)handleData {
    
    if (1 == self.page) {
        self.arrayForFood = [NSMutableArray array];
    }
    NSString *url = [NSString stringWithFormat:@"http://api.meishixing.com/place/food/foodlist/place_id=%@&page=%ld", self.myId, self.page];
    [AFNetHandle GETWithURL:url body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        NSDictionary *dictionary = [NSDictionary dictionaryWithDictionary:result];
        NSArray *arr = [dictionary objectForKey:@"result"];

        for (NSDictionary *dic in arr) {
            GFOtherFoodModel *model = [GFOtherFoodModel modWithDic:dic];
            [self.arrayForFood addObject:model];
        }
        if (self.arrayForFood.count != 0) {
            [self.foodListCollectionView reloadData];
        }

        if (arr.count != 0) {
            self.foodListCollectionView.footer.hidden = NO;
            if (self.arrayForFood.count < 10) {
                self.foodListCollectionView.footer.hidden = YES;
            } else {
                self.foodListCollectionView.footer.hidden = NO;
            }
        } else {
            self.foodListCollectionView.footer.hidden = YES;
        }
        [self.waitView removeFromSuperview];
        [self.overTimeView removeFromSuperview];
    } fail:^(NSError *error) {
        [self.overTimeView removeFromSuperview];
        self.overTimeView = [[GFOverTimeView alloc] initWithFrame:self.view.frame withButtonBlock:^(UIButton *button) {
            [self.view addSubview:self.waitView];
            [self handleData];
        }];
        [self.waitView removeFromSuperview];
        [self.view addSubview:self.overTimeView];
    }];
}
#pragma mark - 创建collectionView
- (void)creatCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    flowLayout.itemSize = CGSizeMake((ScreenWidth - 15) / 2, ScreenHeight /3);
    self.foodListCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) collectionViewLayout:flowLayout];
    self.foodListCollectionView.dataSource = self;
    self.foodListCollectionView.delegate = self;
    self.foodListCollectionView.backgroundColor = [UIColor lightGrayColor];
    [self.foodListCollectionView registerClass:[GFFoodLIstCollectionViewCell class] forCellWithReuseIdentifier:@"foodList"];
    self.foodListCollectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footRefresh:)];
    self.foodListCollectionView.footer.hidden = YES;
    [self.view addSubview:self.foodListCollectionView];
}
#pragma mark - 上拉加载
- (void)footRefresh:(MJRefreshFooter *)footer {
    [self.view addSubview:self.waitView];
    self.page ++;
    [self.foodListCollectionView.footer beginRefreshing];
    [self.foodListCollectionView.footer endRefreshing];
    [self handleData];
    
}
#pragma mark - item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GFFoodLIstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"foodList" forIndexPath:indexPath];
    if (self.arrayForFood.count != 0) {
        GFOtherFoodModel *model = [self.arrayForFood objectAtIndex:indexPath.item];
        cell.layer.masksToBounds = YES;
        cell.layer.cornerRadius = 5;
        cell.arraySort = self.arrayForSort;
        cell.model = model;
    }

    return cell;
}
#pragma mark - item number
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arrayForFood.count;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    GFOtherFoodModel *model = [self.arrayForFood objectAtIndex:indexPath.item];
    GFNearHomeDetialViewController *detialVC = [[GFNearHomeDetialViewController alloc] init];
    detialVC.i = 300;
    detialVC.tweet_id = model.tweet_id;
    [self.navigationController pushViewController:detialVC animated:YES];
    
}

#pragma mark - 视图加载
- (void)viewDidLoad {
    [super viewDidLoad];
    [self viewForNavi];
    [self creatCollectionView];
    [self.view bringSubviewToFront:self.waitView];
    [self handleData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
